import { action, observable, toJS } from "mobx";
import { Toast, Tools } from 'weaver-mobile';
import app from '../../apis/app';
import ListViewStore from "../../common/list-view/Store";
import { dateType } from '../../components/form/statistics/typeof';
import { TIPS_MSG } from '../../constants/form';
import AppChartStore from "../../stores/app/analysis/ChartStore";
import { BrowserHrm , Browser } from 'weaver-mobile-page';
import _u from '../../utils';
import _ from 'lodash';

const { BYDAY, BYALL, BYWEEK } = dateType;
const FillinFrequency  = {
  unlimit: 'UNLIMIT',
  limitOnce: 'LIMIT_ONCE',
  dailyRepeat: 'DAILY_REPEAT'
};
const getLabel = Tools.getLabel; 

export default class StatisticsStore {
  @observable appid = null;
  @observable appMsg = {};
  @observable submitResult = {};
  @observable eachDayCount = null;
  @observable fillinEnable = true;
  @observable date = _u.dateFormat('YY-mm-dd', new Date());
  @observable dateType = BYDAY;
  @observable loading = false;
  @observable chartStore = new AppChartStore();
  @observable submittedStore = new ListViewStore();
  @observable browserHrm = new BrowserHrm.Store({ route: '/form/statistics', id: this.appid });
  @observable browser = new Browser.Store({ route: '/form/statistics', id: this.appid });
  @observable conditions = { deptId: '', userId: '' };

  @action init = (appid) => {
    this.appid = appid;
    this.loading = true;

    app.getSetInfo(appid).then(res => {
      if (res.fillinFrequency === FillinFrequency.unlimit || res.fillinFrequency === FillinFrequency.limitOnce) {
        this.dateType = BYALL;
        this.date = '';
      } else if (res.fillinFrequency === FillinFrequency.dailyRepeat) {
        this.dateType = BYDAY;

        this.date = _u.dateFormat('YY-mm-dd', new Date());
      } else {
        this.dateType = BYWEEK;
        let endDate = new Date();
        let startDate = new Date();

        startDate.setDate(startDate.getDate() - startDate.getDay() + 1);

        if (endDate.getDay() === 0) {
          endDate = endDate;
        } else {
          endDate.setDate(endDate.getDate() + 7 - endDate.getDay());
        }

        const date = [_u.dateFormat('YY-mm-dd', startDate), _u.dateFormat('YY-mm-dd', endDate)];
        this.date = date;
      }

      Promise.all(
        [
          app.getBaseInfo(appid),
          app.getCountInfo({
            appid,
            date: this.date
          }),
          this.getSubmittedInfo(appid),
          app.getCountInfoEachDay({appid}),
        ]
      ).then(([data, countInfo, submittedInfo, eachDayCount]) => {
        this.appMsg = data;
        this.submitResult = countInfo;
        this.eachDayCount = eachDayCount;
        this.fillinEnable = this.appMsg.fillinEnable;
        this.loading = false;
      });
    });

    
    this.chartStore.initState();
  }

  @action setDate = (type, value, selectedIndex, disableLoading, conditions ) => {
    this.dateType = type;

    if (conditions) {
      this.conditions = conditions;
    }

    switch (type) {
      case BYDAY:
        this.date = value;
        break;
      case BYALL:
        this.date = '';
        break;
      case BYWEEK:
        this.date = value;
      default:
        break;
    }

    !disableLoading && Toast.loading(TIPS_MSG.LOADING, 0);

    Promise.all([
      app.getCountInfo({ appid: this.appid, date: this.date }),
      selectedIndex === 0 && this.getSubmittedInfo(this.appid)
    ]).then(([countInfo]) => {
      this.submitResult = countInfo;

      if (selectedIndex === 1) {
        this.chartStore.init(this.appid, this.date);
      }

      !disableLoading && Toast.hide();
    });
  }

  onFillinEnable = () => {
    const enable = toJS(this.fillinEnable);
    app.fillinEnable({
      appid: this.appid,
      enable: !enable
    }).then(() => {
      this.fillinEnable = !enable;
      Toast.success(getLabel(523251, "操作成功"), 1);
    });
  }

  getSubmittedInfo = (appid) => {
    return this.submittedStore.getData(() => {
      return app.getSubmittedInfo({
        appid,
        date: this.date,
        userId: this.conditions.userId,
        deptId: this.conditions.deptId
      }).then(result => {
        return result.dataKey;
      });
    });
  }
}


